import 'package:flutter/material.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/server/api/user_api.dart';
import 'package:sports_new/utils/toast_utils.dart';
import 'package:sports_new/utils/user.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:sports_new/widgets/base/qkd_button.dart';

class ColorSetting{
  ColorSetting({
    required this.backgroundColor,
    required this.textColor,
    this.borderColor
  });

  Color backgroundColor;
  Color textColor;
  Color? borderColor;
}

class FocusButton extends StatefulWidget {
  // const FocusButton({super.key});

  int focus;
  String? userId;
  String? expertId;
  //0 纯色  1边框
  int type;
  ColorSetting? focusColorSetting;
  ColorSetting? unFocusColorSetting;
  double width;
  double height;
  final double radius;
  final String? focusText;
  final String? unFocusText;
  final double? fontSize;
  Function(int?)? onTap;

  FocusButton(this.focus,
      {super.key,
      this.type = 0,
      this.userId,
      this.expertId,
      this.width = 56,
      this.height = 24,
      this.focusColorSetting,
      this.unFocusColorSetting,
      this.onTap,
      this.radius = 4,
      this.focusText,
      this.unFocusText,
      this.fontSize});

  @override
  State<FocusButton> createState() => _FocusButtonState();
}

class _FocusButtonState extends State<FocusButton> {
  @override
  Widget build(BuildContext context) {
    ColorSetting focusSetting = widget.focusColorSetting ?? ColorSetting(
      backgroundColor: widget.type == 0?Colours.main:Colours.transparent,
      textColor: widget.type == 0?Colours.white:Colours.main,
      borderColor: widget.type == 0?null:Colours.main
    );
    ColorSetting unFocusSetting = widget.unFocusColorSetting ?? ColorSetting(
      backgroundColor: widget.type == 0?Colours.greyF2:Colours.transparent,
      textColor: widget.type == 0?Colours.grey99:Colours.greyCC,
      borderColor: widget.type == 0?null:Colours.greyCC
    );
    return (User.info != null && User.info?.id == widget.userId)
        ? Container()
        : QkdButton(
            text: widget.focus == 0 ? (widget.focusText??'关注') : (widget.unFocusText??'已关注'),
            strutStyle: Styles.centerStyle(fontSize: 16),
            textStyle: TextStyle(
              color: widget.focus == 0 ?focusSetting.textColor:unFocusSetting.textColor,
              fontSize: widget.fontSize??13,
              fontWeight: FontWeight.w400
            ),
            borderSide: widget.focus == 0?
              focusSetting.borderColor != null?
              BorderSide(color: focusSetting.borderColor!, width: 0.5):BorderSide.none:
              unFocusSetting.borderColor != null?
              BorderSide(color: unFocusSetting.borderColor!, width: 0.5):BorderSide.none,
            borderRadius: widget.radius,
            height: widget.height,
            width: widget.width,
            backgroundColor: widget.focus == 0?focusSetting.backgroundColor:unFocusSetting.backgroundColor,
            onPressed: () async {
              if (widget.focus == 0) {
                final result = await UserApi.focus(
                    userId: widget.userId, expertId: widget.expertId
                );
                if (result == 200) {
                  ToastUtils.show('关注成功');
                  widget.focus = 1;
                  update();
                  if (widget.onTap != null) {
                    widget.onTap!(1);
                  }
                  return;
                }
              } else {
                final r = await WidgetUtils.alertQuery('确认不再关注？');
                if (r == true) {
                  final result = await UserApi.unFocus(
                      userId: widget.userId, expertId: widget.expertId
                  );
                  if (result == 200) {
                    ToastUtils.show('取消关注成功');
                    widget.focus = 0;
                    update();
                    if (widget.onTap != null) {
                      widget.onTap!(0);
                    }
                    return;
                  }
                }
              }
            });
  }
}
